﻿using Microsoft.Xna.Framework;
using Phoenix.Xna.Framework;
using Phoenix.Xna.Framework.Components.Cameras;
using Phoenix.Xna.Framework.Components.Terrain;
using Phoenix.Xna.Framework.Input;
using Phoenix.Xna.Framework.Input.Binding;
using Phoenix.Xna.Framework.Input.Mice;
using Phoenix.Xna.Framework.Input.Keyboards;

namespace EngineTest.Components.Cameras
{
    public class RPGCamera : FreeCamera
    {
        private bool _leftMouseButtonDown;

        public ProgressiveTerrain Terrain { get; set; }

        public RPGCamera()
            : base()
        {
        }

        public RPGCamera(string id)
            : base(id)
        {
        }

        public RPGCamera(string id, float fov, float nearClip, float farClip)
            : base(id, fov, nearClip, farClip)
        {
        }

        protected override void OnAxisChanged(object sender, InputMapEventArgs<MouseAxisMap> e)
        {
            if (_leftMouseButtonDown)
            {
                base.OnAxisChanged(sender, e);
            }
        }

        private void BindInput()
        {
            InputBindingManager.Instance.BindInputMap<KeyboardKeyMap>("Camera", "Forward", InputEvent.Pressed, OnMoveForward);
            InputBindingManager.Instance.BindInputMap<KeyboardKeyMap>("Camera", "Backward", InputEvent.Pressed, OnMoveBackward);
            InputBindingManager.Instance.BindInputMap<KeyboardKeyMap>("Camera", "Left", InputEvent.Pressed, OnMoveLeft);
            InputBindingManager.Instance.BindInputMap<KeyboardKeyMap>("Camera", "Right", InputEvent.Pressed, OnMoveRight);
            InputBindingManager.Instance.BindInputMap<KeyboardKeyMap>("Camera", "Forward", InputEvent.Held, OnMoveForward);
            InputBindingManager.Instance.BindInputMap<KeyboardKeyMap>("Camera", "Backward", InputEvent.Held, OnMoveBackward);
            InputBindingManager.Instance.BindInputMap<KeyboardKeyMap>("Camera", "Left", InputEvent.Held, OnMoveLeft);
            InputBindingManager.Instance.BindInputMap<KeyboardKeyMap>("Camera", "Right", InputEvent.Held, OnMoveRight);
            InputBindingManager.Instance.BindInputMap<MouseButtonMap>("Camera", "LeftButtonDown", InputEvent.Pressed, OnLeftMouseButtonPressed);
            InputBindingManager.Instance.BindInputMap<MouseButtonMap>("Camera", "LeftButtonDown", InputEvent.Released, OnLeftMouseButtonReleased);
            InputBindingManager.Instance.BindInputMap<MouseAxisMap>("Camera", "Axis", InputEvent.ValueChanged, OnAxisChanged);
        }

        private void OnLeftMouseButtonPressed(object sender, InputMapEventArgs<MouseButtonMap> e)
        {
            _leftMouseButtonDown = true;
        }

        private void OnLeftMouseButtonReleased(object sender, InputMapEventArgs<MouseButtonMap> e)
        {
            _leftMouseButtonDown = false;
        }

        #region IComponent Members

        public override void Initialize()
        {
            base.Initialize();
            BindInput();
        }

        public override void Update(GameTime gameTime)
        {
            //Camera.ActiveCamera.SetPosition(null,
            //    Terrain.GetHeight(Position.X, Position.Z) + 30,
            //    null);
            base.Update(gameTime);
        }

        #endregion
    }
}
